package com.hait.mypicture.controller;

import com.hait.mypicture.common.BaseResponse;
import com.hait.mypicture.common.ResultUtils;
import com.hait.mypicture.exception.ErrorCode;
import com.hait.mypicture.exception.ThrowUtils;
import com.hait.mypicture.model.dto.space.analyze.*;
import com.hait.mypicture.model.entity.Space;
import com.hait.mypicture.model.entity.User;
import com.hait.mypicture.model.vo.space.analyze.*;
import com.hait.mypicture.service.SpaceAnalyzeService;
import com.hait.mypicture.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/space/analyze")
public class SpaceAnalyzeController {

    @Resource
    private UserService userService;

    @Resource
    private SpaceAnalyzeService spaceAnalyzeService;

    /**
     * 分析空间使用情况
     *
     * @param spaceAnalyzeRequest 空间分析请求参数
     * @param request             请求
     * @return 空间使用情况
     */
    @PostMapping("/usage")
    public BaseResponse<SpaceUsageAnalyzeResponse> getSpaceUsageAnalyze(@RequestBody SpaceUsageAnalyzeRequest spaceAnalyzeRequest, HttpServletRequest request) {
        // 参数校验
        ThrowUtils.throwIf(spaceAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        SpaceUsageAnalyzeResponse analyzeResponse = spaceAnalyzeService.analyzeSpaceUsage(spaceAnalyzeRequest, loginUser);
        return ResultUtils.success(analyzeResponse);
    }

    /**
     * 分析空间分类情况
     *
     * @param spaceCategoryAnalyzeRequest 空间分类分析请求参数
     * @param request                     请求
     * @return 空间分类情况
     */
    @PostMapping("/category")
    public BaseResponse<List<SpaceCategoryAnalyzeResponse>> getSpaceCategoryAnalyze(@RequestBody SpaceCategoryAnalyzeRequest spaceCategoryAnalyzeRequest, HttpServletRequest request) {
        // 参数校验
        ThrowUtils.throwIf(spaceCategoryAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceCategoryAnalyzeResponse> spaceCategoryAnalyzeResponses = spaceAnalyzeService.analyzeSpaceCategory(spaceCategoryAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceCategoryAnalyzeResponses);
    }

    /**
     * 分析空间标签情况
     *
     * @param spaceTagAnalyzeRequest 空间标签分析请求参数
     * @param request                请求
     * @return 空间标签情况
     */
    @PostMapping("/tag")
    public BaseResponse<List<SpaceTagAnalyzeResponse>> getSpaceTagAnalyze(@RequestBody SpaceTagAnalyzeRequest spaceTagAnalyzeRequest, HttpServletRequest request) {
        // 参数校验
        ThrowUtils.throwIf(spaceTagAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceTagAnalyzeResponse> spaceTagAnalyzeResponses = spaceAnalyzeService.analyzeSpaceTag(spaceTagAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceTagAnalyzeResponses);
    }

    /**
     * 分析空间大小情况
     *
     * @param spaceSizeAnalyzeRequest 空间大小分析请求参数
     * @param request                 请求
     * @return 空间大小情况
     */
    @PostMapping("/size")
    public BaseResponse<List<SpaceSizeAnalyzeResponse>> getSpaceSizeAnalyze(@RequestBody SpaceSizeAnalyzeRequest spaceSizeAnalyzeRequest, HttpServletRequest request) {
        // 参数校验
        ThrowUtils.throwIf(spaceSizeAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceSizeAnalyzeResponse> spaceSizeAnalyzeResponses = spaceAnalyzeService.analyzeSpaceSize(spaceSizeAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceSizeAnalyzeResponses);
    }

    /**
     * 分析空间用户
     *
     * @param spaceUserAnalyzeRequest 空间用户分析请求
     * @param request                 请求
     * @return 分析空间用户响应
     */
    @PostMapping("/user")
    public BaseResponse<List<SpaceUserAnalyzeResponse>> analyzeSpaceUser(@RequestBody SpaceUserAnalyzeRequest spaceUserAnalyzeRequest,
                                                                         HttpServletRequest request) {
        // 参数校验
        ThrowUtils.throwIf(spaceUserAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceUserAnalyzeResponse> spaceUserAnalyze = spaceAnalyzeService.getSpaceUserAnalyze(spaceUserAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceUserAnalyze);
    }

    /**
     * 获取空间用户分析
     *
     * @param spaceRankAnalyzeRequest 请求参数
     * @param request                 请求
     * @return 响应
     */
    @PostMapping("/rank")
    public BaseResponse<List<Space>> getSpaceRankAnalyze(@RequestBody SpaceRankAnalyzeRequest spaceRankAnalyzeRequest,
                                                         HttpServletRequest request) {
        // 参数校验
        ThrowUtils.throwIf(spaceRankAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<Space> spaceList = spaceAnalyzeService.getSpaceRankAnalyze(spaceRankAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceList);
    }
}